package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.OverflowListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OverflowListServiceImpl implements OverflowListService {

    @Autowired
    private OverflowListDao overflowListDao;
    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;
    @Override
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        if (user!=null) {
            overflowList.setUserId(user.getUserId());
        }
        overflowListDao.save(overflowList);
        List<OverflowListGoods> overflowListGoods = JSON.parseArray(overflowListGoodsStr, OverflowListGoods.class);
        for (OverflowListGoods overflowListGood : overflowListGoods) {
//            OverflowList overflowList1 =overflowListDao.selectOverflowList(overflowList.getOverflowNumber());
            overflowListGood.setOverflowListId(overflowList.getOverflowListId());
            overflowListGoodsDao.save(overflowListGood);
        }
        return new ServiceVO(100,"请求成功");
    }

    /**
     * 报溢单查询
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        List<OverflowList> overflowLists = overflowListDao.list(sTime,eTime);
        for (OverflowList overflowList : overflowLists) {
            User user = overflowListDao.getUser(overflowList.getUserId());
            if (user!=null){
                overflowList.setTrueName(user.getTrueName());
            }else {
                overflowList.setTrueName(null);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rows",overflowLists);
        return map;
    }

    /**
     * 报溢单商品信息
     * @param overflowListId
     * @return
     */
    @Override
    public Map<String, Object> goodList(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoodsList =  overflowListGoodsDao.goodList(overflowListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",overflowListGoodsList);
        return map;
    }
}
